package cn.ekuma.android;

import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

/**
 * Created by Administrator on 2014/10/11 0011.
 */
public class AdapterFactory {


    public static Adapter inversion(final Adapter src){
        return new Adapter(){

            @Override
            public void registerDataSetObserver(DataSetObserver observer) {
                src.registerDataSetObserver(observer);
            }

            @Override
            public void unregisterDataSetObserver(DataSetObserver observer) {
                 src.unregisterDataSetObserver(observer);
            }

            @Override
            public int getCount() {
                return src.getCount();
            }

            @Override
            public Object getItem(int position) {
                return src.getItem(getRealPosition(position));
            }

            @Override
            public long getItemId(int position) {
                return src.getItemId(getRealPosition(position));
            }

            @Override
            public boolean hasStableIds() {
                return src.hasStableIds();
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                return src.getView(getRealPosition(position),convertView,parent);
            }

            @Override
            public int getItemViewType(int position) {
                return src.getItemViewType(getRealPosition(position));
            }

            @Override
            public int getViewTypeCount() {
                return src.getViewTypeCount();
            }

            @Override
            public boolean isEmpty() {
                return src.isEmpty();
            }


            private int getRealPosition(int position){
                return getCount()-position-1;
            }
        };
    }

    public static SpinnerAdapter nullAble(final SpinnerAdapter src){
        return new SpinnerAdapter(){

            @Override
            public void registerDataSetObserver(DataSetObserver observer) {
                src.registerDataSetObserver(observer);
            }

            @Override
            public void unregisterDataSetObserver(DataSetObserver observer) {
                src.unregisterDataSetObserver(observer);
            }

            @Override
            public int getCount() {
                return src.getCount()+1;
            }

            @Override
            public Object getItem(int position) {
                if(position==0)
                    return null;
                return src.getItem(getRealPosition(position));
            }

            @Override
            public long getItemId(int position) {
                if(position==0)
                    return position;
                return src.getItemId(getRealPosition(position));
            }

            @Override
            public boolean hasStableIds() {
                return src.hasStableIds();
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if(position==0) {
                    View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, null);
                    TextView text = (TextView) view;
                    text.setText("未选择");
                    return text;
                }
                return src.getView(getRealPosition(position),convertView,parent);
            }

            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                return getView(position, convertView, parent);
            }

            @Override
            public int getItemViewType(int position) {
                return src.getItemViewType(getRealPosition(position));
            }

            @Override
            public int getViewTypeCount() {
                return src.getViewTypeCount();
            }

            @Override
            public boolean isEmpty() {
                return src.isEmpty();
            }


            private int getRealPosition(int position){
                return position-1;
            }
        };
    }
}
